package listener;

import java.util.ArrayList;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;


import entity.User;
import util.sessionUtil;

//@WebListener
public class httpsession_listener implements HttpSessionListener
{
	//当前在线用户
	private int online_userCount = 0;
	
	@Override
	public void sessionCreated(HttpSessionEvent arg0)
	{
//		System.out.println("============sessionCreated=============");
		online_userCount++;
		//#{online_userCount}这样，全局June可以访问
		arg0.getSession().getServletContext().setAttribute("online_userCount", online_userCount);
	}
	
	@Override
	public void sessionDestroyed(HttpSessionEvent arg0)
	{
//		System.out.println("============sessionDestroyed=============");
		online_userCount--;
		arg0.getSession().getServletContext().setAttribute("online_userCount", online_userCount);
		
		ArrayList<User> userList = null; //在线用户list
		userList = (ArrayList<User>)arg0.getSession().getServletContext().getAttribute("userList");
		
		String sessionID = arg0.getSession().getId();
		if(sessionUtil.getUserBySeesionID(userList, sessionID) != null)
		{
			userList.remove(sessionUtil.getUserBySeesionID(userList, sessionID));
		}
	}
	
}
